package com.youme.section.profile

import android.util.Log
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.hyphenate.EMCallBack
import com.hyphenate.easeim.DemoHelper
import com.hyphenate.easeim.R
import com.hyphenate.easeim.section.base.BaseActivity
import com.hyphenate.easeim.section.dialog.SimpleDialogFragment
import com.hyphenate.easeim.section.me.activity.UserDetailActivity
import com.hyphenate.easeui.manager.EaseThreadManager
import com.salton123.ui.base.YoumeBaseFragment
import com.salton123.utils.ImageLoaders
import com.youme.Constants
import com.youme.RouterManager
import com.youme.api.Callback
import com.youme.mCurrentUserProfile
import com.youme.model.Profile
import kotlinx.android.synthetic.main.fragment_profile.*

/**
 * Time:3/31/21 11:20 PM
 * Author:
 * Description:
 */
@Route(path = Constants.Router.YOUME.PROFILE)
class ProfileFragment : YoumeBaseFragment() {
    override fun initViewAndData() {
        displayUserInfo()
    }

    override fun onResume() {
        super.onResume()
        displayUserInfo()
    }

    override fun onSupportVisible() {
        super.onSupportVisible()
        displayUserInfo()
    }

    private fun displayUserInfo() {
        if (mCurrentUserProfile == null) {
            mViewModel.profile(object : Callback<Profile> {
                override fun onSuccess(value: Profile) {
                    mCurrentUserProfile = value
                }

                override fun onFailed(errorCode: Int, errorMessage: String, throwable: Throwable?) {
                    Log.i("errorMessage", "errorMessage : " + errorMessage + "throwable" + throwable)
                }
            })
        }
        tvNickname.text = mCurrentUserProfile?.nickName
        tvTag.text = "${mCurrentUserProfile?.userName}"
        ImageLoaders.loadCircleCrop(ivAvatar, mCurrentUserProfile?.avatar ?: "", R.drawable.ic_default_avatar)
    }

    override fun initListener() {
        super.initListener()
        setListener(ivAvatar, tvNickname, ivEdit, tvTag)
        setListener(tvWatchAllOrder, llAllOrder, llUnpayOrder,
            llOrderDelivering, llOrderFinished, llOrderService,
            llAddress, llHotRest, llSetting, llTest, btnLogout)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v) {
            ivAvatar, tvNickname, ivEdit, tvTag -> {
                shortToast("资料编辑页面")
                UserDetailActivity.actionStart(activity(), mCurrentUserProfile?.nickName ?: "",
                    mCurrentUserProfile?.avatar ?: "")
            }
            tvWatchAllOrder, llAllOrder -> {
                RouterManager.order(activity())
            }

            llUnpayOrder -> {
                shortToast("未实现")
            }

            llOrderDelivering -> {
                shortToast("未实现")
            }
            llOrderFinished -> {
                shortToast("未实现")
            }
            llOrderService -> {
                shortToast("未实现")
            }
            llAddress -> {
                RouterManager.address(activity())
            }
            llHotRest -> {
                shortToast("未实现")
            }
            llSetting -> {
                RouterManager.about(activity())
                shortToast("未实现")
            }
            llTest -> {
                //pass it like this
//                val file = File("/sdcard/01.jpg")
//                val requestFile: RequestBody = RequestBody.create("multipart/form-data".toMediaType(), file)
//                val body: MultipartBody.Part = MultipartBody.Part.createFormData("file", file.name, requestFile)
//                val fullName: RequestBody = RequestBody.create("multipart/form-data".toMediaType(), file.name)
//                lifecycleScope.launch {
//                    println(mYouMeApi.upload(body).body())
//                }
            }
            btnLogout -> {
                logout()
            }
        }
    }

    private fun logout() {
        SimpleDialogFragment.Builder(requireActivity() as BaseActivity?)
            .setTitle(R.string.em_login_out_hint)
            .showCancelButton(true)
            .setOnConfirmClickListener(R.string.em_dialog_btn_confirm) {
                DemoHelper.getInstance().logout(true, object : EMCallBack {
                    override fun onSuccess() {
                        RouterManager.smsLogin(activity())
                        activity().finish()
                    }

                    override fun onError(code: Int, error: String) {
                        EaseThreadManager.getInstance().runOnMainThread { shortToast(error) }
                    }

                    override fun onProgress(progress: Int, status: String) {}
                })
            }
            .show()
    }

    override fun getLayoutId(): Int = R.layout.fragment_profile
}